<?php

declare(strict_types=1);

namespace Shenlink\Holiya\Console\Command;

use Shenlink\Holiya\Console\Command\Repository\GiteeIRepository;
use Shenlink\Holiya\Console\Command\Repository\GithubIRepository;
use Shenlink\Holiya\Console\Command\Repository\IRepository;

/**
 * 推送命令类
 *
 * 用于推送代码到远程仓库
 */
class PushCommand implements ICommand
{
    /**
     * 已注册的仓库列表
     *
     * @var array<string, IRepository>
     */
    private array $repositories = [];

    /**
     * PushCommand 构造函数
     */
    public function __construct()
    {
        // 注册默认仓库实现
        $this->addRepository(new GiteeIRepository());
        $this->addRepository(new GithubIRepository());
    }

    /**
     * 添加仓库到命令中
     *
     * @param IRepository $repository 要添加的仓库
     * @return void
     */
    public function addRepository(IRepository $repository): void
    {
        $this->repositories[$repository->getName()] = $repository;
    }

    /**
     * 获取命令名称
     *
     * @return string 命令名称
     */
    public function getName(): string
    {
        return 'push';
    }

    /**
     * 获取命令描述
     *
     * @return string 命令描述
     */
    public function getDescription(): string
    {
        return '推送代码到远程仓库';
    }

    /**
     * 执行命令
     *
     * @param array $arguments 命令参数
     * @return int 执行结果状态码
     */
    public function execute(array $arguments = []): int
    {
        // 如果没有指定平台，则推送所有已配置的仓库
        if (empty($arguments)) {
            return $this->pushToAllRepositories();
        }

        $platform = $arguments[0];

        // 检查指定的平台是否支持
        if (!isset($this->repositories[$platform])) {
            echo "不支持的平台: $platform\n";
            echo "支持的平台:\n";
            foreach ($this->repositories as $repo) {
                echo "  - " . $repo->getName() . ": " . $repo->getDescription() . "\n";
            }
            return 1;
        }

        // 推送到指定平台
        $repository = $this->repositories[$platform];
        if ($repository->push($arguments)) {
            return 0;
        }

        return 1;
    }

    /**
     * 推送到所有已配置的仓库
     *
     * @return int 执行结果状态码
     */
    private function pushToAllRepositories(): int
    {
        echo "正在推送到所有已配置的仓库...\n";

        $successCount = 0;
        $totalCount = count($this->repositories);

        foreach ($this->repositories as $repository) {
            echo "推送至 " . $repository->getName() . ":\n";
            if ($repository->push()) {
                $successCount++;
            }
            echo "\n";
        }

        echo "推送完成 ($successCount/$totalCount)\n";

        if ($successCount === $totalCount) {
            return 0; // 全部成功
        } elseif ($successCount > 0) {
            return 2; // 部分成功
        } else {
            return 1; // 全部失败
        }
    }

    /**
     * 显示帮助信息
     *
     * @return void
     */
    public function showHelp(): void
    {
        echo "用法: php holiya push [platform]\n\n";
        echo "推送代码到远程仓库。\n\n";
        echo "参数:\n";
        echo "  platform    可选，指定要推送的平台 (如: gitee)\n";
        echo "              如果不指定平台，则推送到所有已配置的仓库\n\n";
        echo "支持的平台:\n";

        foreach ($this->repositories as $repo) {
            echo "  " . $repo->getName() . "    " . $repo->getDescription() . "\n";
        }

        echo "\n示例:\n";
        echo "  php holiya push          # 推送到所有已配置的仓库\n";
        echo "  php holiya push gitee    # 仅推送到 Gitee\n";
        echo "  php holiya push github    # 仅推送到 Gitub\n";
    }
}